---
title: "Balancing algorithm report"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: console
---

```{r,message=FALSE,echo=FALSE}
library(dplyr)
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
df <- read.csv("ireland_clean.csv")

est <- read.csv("Irelandestimates.csv")
est$est=est$est/100
est$Freq=nrow(df) * est$est
est$est=NULL
X <- split(est, est$variable)
X=lapply(X, function(x) { x["variable"] <- NULL; x })
list2env(X ,.GlobalEnv)
joint.d=rename(joint, joint=label)
pol.d=rename(pol, pol=label)
sex.d=rename(sex, sex=label)
vot.d=rename(vot, vot=label)

library(survey)
df$ids=seq(1:nrow(df))
df1 <- svydesign(ids=~1, data=df)

df.r <- rake(design = df1,
             sample.margins = list(~joint, ~pol, ~sex, ~vot),
             population.margins = list(joint.d, pol.d, sex.d, vot.d))

df.r1 <- trimWeights(df.r, lower=0.6, upper=5,
                                   strict=TRUE) 
df$w=weights(df.r1)
df$Freq=round(df$w,0)

df0=select(df,Record.ID,joint,pol,sex,vot,Freq)
MyData <- df0[rep(row.names(df0), df0$Freq), 1:5]
write.csv(MyData,"MyData.csv",row.names=F)

```

# Python (config = code) 
```{python engine.path="/usr/bin/python3.5",results='hide'}
import sys
sys.path.append('/home/shared/data/balancing/')
import drop
config = "{1: {'degreeHigh':10.2,'noDegreeHigh':38.3,'degreeLow':21.7,'noDegreeLow':29.8}},{2: {'interest':50.6,'LowInterest':49.4}},{3: {'Male':48.1,'Female':51.9}},{4: {'FG':18.92,'SF':16.57,'FF':18.95,'LAB':4.52,'GRN':4.18,'IND':16.86,'Undec':20.00}}"

filename = '/home/fernando/Misc/bjpolFinal/Ireland/MyData.csv'

drop.run(config, filename, "/home/fernando/Misc/bjpolFinal/Ireland/myBal", 2)
```
